<?php
/**
 * ProjektMan
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   ProjektMan
 * @name       TaskController.php
 * @license    LICENSE.txt     New BSD License
 */

/**
 * TaskController
 *
 * @category   ProjektMan
 * @name       TaskController
 * @license    LICENSE.txt     New BSD License
 */
class TaskController extends Projekt_Action
{

    /**
     * Index page will redirect to the list page
     * we should not link directly to the index page to avoid unecessary
     * redirects
     *
     */
    public function indexAction ()
    {
        $this->_redirect('/task/list/');
    }

    /**
     * Display a list of tasks and provide a link to edit a task
     *
     */
    public function listAction ()
    {
    
    }

    /**
     * Create form to add and edit a task
     *
     * @param string $task_id
     * @return Zend_Form
     */
    public function taskForm ($task_id = null)
    {
        $formData = null;
        $statuses = null;
        $projects = null;
        $users = null;
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            
            $statusList = new StatusList(array('status_id' => $formData['status_id']));
            $projectList = new ProjectList(array('project_id' => $formData['project_id'], 'fetchPairs' => true));
            $userList = new UserList(array('user_id' => $formData['user_id']));
        } else {
            $statusList = new StatusList(array('status_type' => 'task'));
            $projectList = new ProjectList(array('project_id' => $this->_getParam('project'), 'fetchPairs' => true));
            $userList = new UserList();
        }
        $statuses = $statusList->getData();
        $projects = $projectList->getData();
        $users = $userList->getData();
        
        $form = new Zend_Form();
        
        // Create and configure username element:
        $taskname = $form->createElement('text', 'task_name');
        $taskname->setLabel('Task Name')->setRequired(true);
        $taskdesc = $form->createElement('textarea', 'task_desc');
        $taskdesc->setLabel('Task Desc')->setAttrib('cols', '50')->setAttrib('rows', '5');
        $taskstatus = $form->createElement('select', 'status_id');
        $taskstatus->setLabel("Status")->addMultiOptions($statuses);
        $taskproject = $form->createElement('select', 'project_id');
        $taskproject->setLabel("Project")->addMultiOptions($projects)->setRequired(true);
        $taskowner = $form->createElement('select', 'owner_id');
        $taskowner->setLabel("Owner")->addMultiOptions($users)->setRequired(true);
        
        $startdate = $form->createElement('text', 'start_date');
        $startdate->setLabel("Start Date");
        $enddate = $form->createElement('text', 'end_date');
        $enddate->setLabel("End Date");
        
        // Add elements to form:
        $form->addElement($taskname)->addElement($taskdesc)->addElement($taskstatus)->addElement($taskproject)->addElement($taskowner)->addElement($startdate)->addElement($enddate)->addElement('submit', 'save', array(
            'label' => 'Save'
        ));
        
        if (isset($task_id)) {
            $taskList = new TaskList(array(
                'task_id' => $task_id
            ));
            $taskdata = $taskList->getData();
            $form->populate($taskdata);
            $action = '/task/edit/task/' . $task_id . '/';
        } else {
            $taskdata['project_id'] = $this->_getParam('project');
            $form->populate($taskdata);
            $action = '/task/add/';
        }
        
        $form->setAction($action)->setMethod('post');
        
        if ($this->_request->isPost()) {
            if ($form->isValid($formData)) {
                
                $task = new Task();
                
                $data = array(
                    'task_name' => $formData['task_name'], 
                    'task_desc' => $formData['task_desc'], 
                    'status_id' => $formData['status_id'], 
                    'project_id' => $formData['project_id'], 
                    'owner_id' => $formData['owner_id'], 
                    'start_date' => $formData['start_date'], 
                    'end_date' => $formData['end_date']
                );
                
                if (isset($task_id)) {
                    $task->update($data, "task_id='{$task_id}'");
                } else {
                    $task->insert($data);
                }
                
                $this->_redirect('/project/overview/project/' . $formData['project_id'] . '/');
            } else {
                $form->populate($formData);
            }
        }
        
        return $form;
    }

    /**
     * Display a form for adding a new task
     *
     */
    public function addAction ()
    {
        $this->view->pageTitle = "Add Task";
        $this->view->crumbTrail = "<a href='/project/list/'>Projects</a>::Add Task";
        $this->view->form = $this->taskForm();
    }

    /**
     * Display a form for editing an existing task
     *
     */
    public function editAction ()
    {
        $this->view->pageTitle = "Edit Task";
        $this->view->crumbTrail = "<a href='/project/list/'>Projects</a>::Edit Task";
        $this->view->form = $this->taskForm($this->_getParam('task'));
    }
}